const MyPromise = require("./MyPromise");
// 添加静态方法all
// arr是promise数组
class Promise {
  static all(arr) {
    let res = [];
    let index = 0;
    return new MyPromise((resolve, reject) => {
      // index === arr.length是等待异步结果的！
      function addData(key, val) {
        res[key] = value;
        index++;
        if (index === arr.length) {
          resolve(res);
        }
      }
      for (let i = 0; i < arr.length; i++) {
        let cur = arr[i];
        if (cur instanceof MyPromise) {
          // Promise对象
          cur.then(
            (value) => addData(i, value),
            (reason) => reject(reason)
          );
        } else {
          addData(i, array[i]);
        }
      }
    });
  }
}
